package com.vallis.asfc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vallis.asfc.entity.AsfcMatch;
import com.vallis.asfc.entity.EventOnMatch;
import com.vallis.asfc.entity.MatchEvent;
import com.vallis.asfc.service.AsfcMatchService;
import com.vallis.asfc.service.EventOnMatchService;
import com.vallis.asfc.service.MatchEventService;
import com.vallis.asfc.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vallis
 * @since 2020-08-11
 */
@RestController
@CrossOrigin
@RequestMapping("/event-on-match")
public class EventOnMatchController {

@Autowired
    private EventOnMatchService eventOnMatchService;
@Autowired
private MatchEventService matchEventService;
@Autowired
private AsfcMatchService asfcMatchService;
@PutMapping("getEventOnMatchByMatchId")
public Result getEventOnMatchByMatchId(Long id){
    return getResult(id);

}

@PutMapping("addEventOnMatchByMatchId")
public Result addEventOnMatchByMatchId( Long matchid,@RequestBody Map<String,String[]> array ){

//获取到了此比赛的所有项目
    String[] events = array.get("events");
List<EventOnMatch> eventOnMatches=new ArrayList<>();
    for (int i = 0; i < events.length; i++) {
        Long aLong = Long.valueOf(events[i]);
        EventOnMatch e=new EventOnMatch();
        MatchEvent byId = matchEventService.getById(aLong);
        AsfcMatch asfcMatch=asfcMatchService.getById(matchid);
        e.setEventid(aLong).setEventName(byId.getEventName()).setMatchid(matchid).setMatchName(asfcMatch.getName());
            eventOnMatches.add(e);
    }

    boolean b = eventOnMatchService.saveBatch(eventOnMatches);

    return getResult(matchid);
}

    private Result getResult(Long match_id) {
        List<EventOnMatch> eventOnMatchList = eventOnMatchService.list(new QueryWrapper<EventOnMatch>().eq("matchid", match_id));
        System.out.println(eventOnMatchList.size());
        if (null != eventOnMatchList || eventOnMatchList.size() != 0) {
            eventOnMatchList.forEach(item -> {
                MatchEvent byId = matchEventService.getById(item.getEventid());
                AsfcMatch asfcMatch = asfcMatchService.getById(item.getMatchid());
            
                if (null != asfcMatch && null != byId) {
                    item.setMatchName(asfcMatch.getName()).setEventName(byId.getEventName());
                }
            
            
            });

}

        return new Result(200,"获取成功",eventOnMatchList);
    }

    @PostMapping("updateEvents")
    public Result updateEvents(@RequestBody EventOnMatch eventOnMatch){
        System.out.println(eventOnMatch);
        eventOnMatchService.updateById(eventOnMatch);
       return getResult(eventOnMatch.getMatchid());

    }
@GetMapping("getOneEventOnMatch")
    public Result getOneEventOnMatch(Long id) {
    EventOnMatch byId = eventOnMatchService.getById(id);
    
    MatchEvent ebyId = matchEventService.getById(byId.getEventid());
    AsfcMatch asfcMatch = asfcMatchService.getById(byId.getMatchid());
    if (null != ebyId && null != asfcMatch) {
        byId.setEventName(ebyId.getEventName()).setMatchName(asfcMatch.getName());
        return new Result(200, "修改成功", byId);
    } else {
        return new Result(400, "该比赛或该项目已经被移除", null);
    }
    
    
}
    
    @PutMapping("removeEvents")
    public Result removeEvents(Long id) {
        
        
        System.out.println("需要删除的id是" + id);
        return new Result(200, "删除成功", eventOnMatchService.removeById(id));
    }



}
